package com.suao.fruits.service.impl;

import com.suao.common.constant.DelFlagConst;
import com.suao.common.utils.DateUtils;
import com.suao.common.utils.SecurityUtils;
import com.suao.common.utils.StringUtils;
import com.suao.fruits.domain.FruitsInfo;
import com.suao.fruits.domain.FruitsStockInfo;
import com.suao.fruits.mapper.FruitsStockInfoMapper;
import com.suao.fruits.service.IFruitsInfoService;
import com.suao.fruits.service.IFruitsStockInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 水果入库管理Service业务层处理
 *
 * @author suao
 * @date 2023-03-08
 */
@Service
public class FruitsStockInfoServiceImpl implements IFruitsStockInfoService
{
    @Autowired
    private FruitsStockInfoMapper fruitsStockInfoMapper;
    
    @Autowired
    private IFruitsInfoService fruitsInfoService;

    /**
     * 查询水果入库管理
     *
     * @param id 水果入库管理主键
     * @return 水果入库管理
     */
    @Override
    public FruitsStockInfo selectFruitsStockInfoById(Long id)
    {
        return fruitsStockInfoMapper.selectFruitsStockInfoById(id);
    }

    /**
     * 查询水果入库管理列表
     *
     * @param fruitsStockInfo 水果入库管理
     * @return 水果入库管理
     */
    @Override
    public List<FruitsStockInfo> selectFruitsStockInfoList(FruitsStockInfo fruitsStockInfo)
    {
        return fruitsStockInfoMapper.selectFruitsStockInfoList(fruitsStockInfo);
    }

    /**
     * 新增水果入库管理
     *
     * @param fruitsStockInfo 水果入库管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFruitsStockInfo(FruitsStockInfo fruitsStockInfo)
    {
        fruitsStockInfo.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        fruitsStockInfo.setCreateTime(DateUtils.getNowDate());
        fruitsStockInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        fruitsStockInfo.setUpdateTime(DateUtils.getNowDate());
        int result = fruitsStockInfoMapper.insertFruitsStockInfo(fruitsStockInfo);
        if(result>0)
        {
            //入库 增加库存
            FruitsInfo fruitsInfo = fruitsInfoService.selectFruitsInfoById(fruitsStockInfo.getFruitsId());
            if(StringUtils.isNotNull(fruitsInfo))
            {
                BigDecimal inventory = fruitsInfo.getInventory().add(fruitsStockInfo.getStockNum());
                BigDecimal inventoryTotal = fruitsInfo.getInventoryTotal().add(fruitsStockInfo.getStockNum());
                fruitsInfo.setInventory(inventory);
                fruitsInfo.setInventoryTotal(inventoryTotal);
            }
            fruitsInfoService.updateFruitsInfo(fruitsInfo);
        }
        return result;
    }

    /**
     * 修改水果入库管理
     *
     * @param fruitsStockInfo 水果入库管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateFruitsStockInfo(FruitsStockInfo fruitsStockInfo)
    {
        //1.获取原始销售数量
        FruitsStockInfo old = fruitsStockInfoMapper.selectFruitsStockInfoById(fruitsStockInfo.getId());
        BigDecimal oldStockNum = old.getStockNum();
        fruitsStockInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        fruitsStockInfo.setUpdateTime(DateUtils.getNowDate());
        int result = fruitsStockInfoMapper.updateFruitsStockInfo(fruitsStockInfo);
        if(result>0)
        {
            //减掉库存
            FruitsInfo fruitsInfo = fruitsInfoService.selectFruitsInfoById(fruitsStockInfo.getFruitsId());
            if(StringUtils.isNotNull(fruitsInfo))
            {
                //先把库存加回去再减掉最新的销售数量
                BigDecimal inventory = fruitsInfo.getInventory().subtract(oldStockNum).add(fruitsStockInfo.getStockNum());
                BigDecimal inventoryTotal = fruitsInfo.getInventoryTotal().subtract(oldStockNum).add(fruitsStockInfo.getStockNum());
                fruitsInfo.setInventory(inventory);
                fruitsInfo.setInventoryTotal(inventoryTotal);
            }
            fruitsInfoService.updateFruitsInfo(fruitsInfo);
        }
        return result;
    }

    /**
     * 批量删除水果入库管理
     *
     * @param ids 需要删除的水果入库管理主键
     * @return 结果
     */
    @Override
    public int deleteFruitsStockInfoByIds(Long[] ids)
    {
        int count =0;
        for(Long id:ids)
        {
            updateFruitsStockInfoDelFlag(id);
            count++;
        }
        return count;
    }

    /**
     * 删除水果入库管理信息
     *
     * @param id 水果入库管理主键
     * @return 结果
     */
    @Override
    public int deleteFruitsStockInfoById(Long id)
    {
        updateFruitsStockInfoDelFlag(id);
        return 1;
    }
    
    @Transactional
    public void  updateFruitsStockInfoDelFlag(Long id)
        {
            FruitsStockInfo temp = fruitsStockInfoMapper.selectFruitsStockInfoById(id);
            temp.setDelFlag(DelFlagConst.DELETED);
            temp.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
            temp.setUpdateTime(DateUtils.getNowDate());
            fruitsStockInfoMapper.updateFruitsStockInfo(temp);
            
            //+回库存
            FruitsInfo fruitsInfo = fruitsInfoService.selectFruitsInfoById(temp.getFruitsId());
            if(StringUtils.isNotNull(fruitsInfo))
            {
                //删除入库，要把当前的入库数量从总库存中删除
                BigDecimal inventory = fruitsInfo.getInventory().subtract(temp.getStockNum());
                BigDecimal inventoryTotal = fruitsInfo.getInventoryTotal().subtract(temp.getStockNum());
                fruitsInfo.setInventory(inventory);
                fruitsInfo.setInventoryTotal(inventoryTotal);
            }
            fruitsInfoService.updateFruitsInfo(fruitsInfo);
        }
}
